<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>state练习-简易轮播图</title>
    <style>
        *{
            margin:0;
            padding:0;
            box-sizing: border-box;
            /* 不可选中 */
            user-select: none;
        }
        body{
            height: 100%;
            background: radial-gradient(#ccc, rgb(58, 94, 141));
        }
        .container{
            width: 640px;
            height: 360px;
            margin:100px auto;
            position:relative;
        }

        .left, .right{
            width: 60px;height:60px;background-color:white;color:black;
            font-size: 30px;font-weight: bold;
            position:absolute;
            top: 150px;
            border-radius: 50%;
            text-align: center;
            line-height: 55px;
            cursor:pointer;
        }
        .left{
            left: 10px;
        }
        .right{
            right: 10px;
        }
    </style>
</head>
<body>
    <div id="root"> </div>

    <script src="../js/react.development.js"></script>
    <script src="../js/react-dom.development.js"></script>
    <script src="../js/babel.min.js"></script>

    <script type="text/babel">
        // less than     gt  greater than
        class App extends React.Component {
            //组件状态
            state = {
                //所有图片
                images: [
                    './images/1.jpg',
                    './images/2.jpg',
                    './images/3.jpg',
                    './images/4.jpg',
                    './images/5.jpg',
                ], 
                //当前图片
                index: 0
            }

            render(){
                //获取 images 和 index 的值
                let {images, index} = this.state;
                //返回虚拟 DOM 对象
                return <div className="container">
                        <img width="100%" src={images[index]} alt="" />
                        <div className="left" onClick={this.prev}>&lt;</div>
                        <div className="right" onClick={this.next}>&gt;</div>
                    </div>
            }

            //上一张
            prev = () => {
                //获取index
                let {index, images} = this.state;
                //判断
                if(index <= 0){  
                    index = images.length - 1;//最后一张
                }else{
                    index -= 1;
                }
                this.setState({
                    index: index
                })
            }

            //下一张
            next = () => {
                //获取index
                let {index, images} = this.state;
                //判断
                if(index >= images.length - 1){  
                    index = 0;
                }else{
                    index += 1;
                }
                this.setState({
                    index: index
                })
            }
        }

        ReactDOM.render(<App />, document.querySelector("#root"))
    </script>
</body>
</html>